package com.anche.cywebnew.Service.securityManager.impl;

import cn.hutool.core.date.DateUtil;
import com.anche.cywebnew.DAO.securityManager.SecurityLogMapper;
import com.anche.cywebnew.Pojo.securityManager.SecurityLog;
import com.anche.cywebnew.Pojo.systemManager.SysUser;
import com.anche.cywebnew.Service.securityManager.SecurityLogService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class SecurityLogServiceImpl implements SecurityLogService {

    @Autowired
    private SecurityLogMapper securityLogMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public Page<SecurityLog> getSecurityLogPager(Integer page, Integer limit, SecurityLog securityLog) {
        Page<SecurityLog> page1 = PageHelper.startPage(page, limit);
        List<SecurityLog> securityLogs = securityLogMapper.selectSecurityLogList(securityLog);
        for (SecurityLog log : securityLogs) {
            String jyw = DigestUtils.md5Hex(log.getLogtype() + log.getOperate_time());
            if (!jyw.equals(log.getJyw())){
                log.setIsupdate(1);
            }
        }
        return page1;
    }

    /**
     * 高频访问、用户超期、非常规业务办理、非正常时间访问、用户解锁、非法ip登录、数据篡改
     * @param logType
     * @param operate_content
     */
    @Override
    public void insertSecurityLog(String logType,  String operate_content, SysUser sysUser) {
        SecurityLog securityLog = new SecurityLog();
        if(Objects.nonNull(sysUser)){
            securityLog.setUser_name(sysUser.getLoginname());
            securityLog.setOrganization(sysUser.getOrgan());
        }

        securityLog.setLogtype(logType);
        securityLog.setTerminal_id(getIpAddr(request));
        securityLog.setOperate_content(operate_content);
        securityLog.setTerminal_type("1");
        securityLog.setOperate_time(DateUtil.format(new Date(), "yyyyMMddHHmmss"));
        String jyw = DigestUtils.md5Hex(logType + securityLog.getOperate_time());
        securityLog.setJyw(jyw);

        securityLogMapper.insertSecurityLogInfo(securityLog);
    }

    @Override
    public List<Map<String, String>> getSecurityLogStat() {
        return securityLogMapper.selectSecurityLogStat(null, null);
    }

    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip.equals("0:0:0:0:0:0:0:1")) {
            ip = "127.0.0.1";
        }
        return ip;
    }
}
